package system

import (
	"context"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/model/system"
	systemReq "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
	"go.uber.org/zap"
	"io"
	"os"
	"path"
	"strings"
)

type Decimal_deviceApi struct{}

// CreateDecimal_device 创建医疗设备
// @Tags Decimal_device
// @Summary 创建医疗设备
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body system.Decimal_device true "创建医疗设备"
// @Success 200 {object} response.Response{msg=string} "创建成功"
// @Router /decimal_device/createDecimal_device [post]
func (decimal_deviceApi *Decimal_deviceApi) CreateDecimal_device(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	var decimal_device system.Decimal_device
	err := c.ShouldBindJSON(&decimal_device)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = decimal_deviceService.CreateDecimal_device(ctx, &decimal_device)
	if err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("创建成功", c)
}

// DeleteDecimal_device 删除医疗设备
// @Tags Decimal_device
// @Summary 删除医疗设备
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body system.Decimal_device true "删除医疗设备"
// @Success 200 {object} response.Response{msg=string} "删除成功"
// @Router /decimal_device/deleteDecimal_device [delete]
func (decimal_deviceApi *Decimal_deviceApi) DeleteDecimal_device(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	ID := c.Query("ID")
	err := decimal_deviceService.DeleteDecimal_device(ctx, ID)
	if err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("删除成功", c)
}

// DeleteDecimal_deviceByIds 批量删除医疗设备
// @Tags Decimal_device
// @Summary 批量删除医疗设备
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{msg=string} "批量删除成功"
// @Router /decimal_device/deleteDecimal_deviceByIds [delete]
func (decimal_deviceApi *Decimal_deviceApi) DeleteDecimal_deviceByIds(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	IDs := c.QueryArray("IDs[]")
	err := decimal_deviceService.DeleteDecimal_deviceByIds(ctx, IDs)
	if err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("批量删除成功", c)
}

// UpdateDecimal_device 更新医疗设备
// @Tags Decimal_device
// @Summary 更新医疗设备
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body system.Decimal_device true "更新医疗设备"
// @Success 200 {object} response.Response{msg=string} "更新成功"
// @Router /decimal_device/updateDecimal_device [put]
func (decimal_deviceApi *Decimal_deviceApi) UpdateDecimal_device(c *gin.Context) {
	// 从ctx获取标准context进行业务行为
	ctx := c.Request.Context()

	var decimal_device system.Decimal_device
	err := c.ShouldBindJSON(&decimal_device)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = decimal_deviceService.UpdateDecimal_device(ctx, decimal_device)
	if err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("更新成功", c)
}

// FindDecimal_device 用id查询医疗设备
// @Tags Decimal_device
// @Summary 用id查询医疗设备
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param ID query uint true "用id查询医疗设备"
// @Success 200 {object} response.Response{data=system.Decimal_device,msg=string} "查询成功"
// @Router /decimal_device/findDecimal_device [get]
func (decimal_deviceApi *Decimal_deviceApi) FindDecimal_device(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	ID := c.Query("ID")
	redecimal_device, err := decimal_deviceService.GetDecimal_device(ctx, ID)
	if err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败:"+err.Error(), c)
		return
	}
	response.OkWithData(redecimal_device, c)
}

// GetDecimal_deviceList 分页获取医疗设备列表
// @Tags Decimal_device
// @Summary 分页获取医疗设备列表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data query systemReq.Decimal_deviceSearch true "分页获取医疗设备列表"
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
// @Router /decimal_device/getDecimal_deviceList [get]
func (decimal_deviceApi *Decimal_deviceApi) GetDecimal_deviceList(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	var pageInfo systemReq.Decimal_deviceSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, total, err := decimal_deviceService.GetDecimal_deviceInfoList(ctx, pageInfo)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败:"+err.Error(), c)
		return
	}
	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, "获取成功", c)
}

// GetDecimal_devicePublic 不需要鉴权的医疗设备接口
// @Tags Decimal_device
// @Summary 不需要鉴权的医疗设备接口
// @Accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{data=object,msg=string} "获取成功"
// @Router /decimal_device/getDecimal_devicePublic [get]
func (decimal_deviceApi *Decimal_deviceApi) GetDecimal_devicePublic(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	// 此接口不需要鉴权
	// 示例为返回了一个固定的消息接口，一般本接口用于C端服务，需要自己实现业务逻辑
	decimal_deviceService.GetDecimal_devicePublic(ctx)
	response.OkWithDetailed(gin.H{
		"info": "不需要鉴权的医疗设备接口信息",
	}, "获取成功", c)
}

// UploadImage 图片上传
// @Tags Decimal_device
// @Summary 图片上传
// @Accept application/json
// @Produce application/json
// @Param data query systemReq.Decimal_deviceSearch true "成功"
// @Success 200 {object} response.Response{data=object,msg=string} "成功"
// @Router /decimal_device/uploadImage [POST]
func (decimal_deviceApi *Decimal_deviceApi) UploadImage(c *gin.Context) {
	// 解析请求中的文件
	file, header, err := c.Request.FormFile("file")
	if err != nil {
		global.GVA_LOG.Error("图片解析失败", zap.Error(err))
		response.FailWithMessage("图片解析失败", c)
		return
	}
	defer file.Close()

	// 验证文件类型
	fileExt := strings.ToLower(path.Ext(header.Filename))
	if fileExt != ".jpg" && fileExt != ".jpeg" && fileExt != ".png" {
		global.GVA_LOG.Error("不支持的图片格式", zap.String("filename", header.Filename))
		response.FailWithMessage("仅支持JPG/JPEG/PNG格式图片", c)
		return
	}

	// 生成唯一文件名
	filename := uuid.New().String() + fileExt
	filePath := path.Join("uploads", filename)

	// 确保上传目录存在
	if err := os.MkdirAll(path.Dir(filePath), 0755); err != nil {
		global.GVA_LOG.Error("创建上传目录失败", zap.Error(err))
		response.FailWithMessage("服务器错误", c)
		return
	}

	// 保存文件到本地
	dst, err := os.Create(filePath)
	if err != nil {
		global.GVA_LOG.Error("创建文件失败", zap.Error(err))
		response.FailWithMessage("文件保存失败", c)
		return
	}
	defer dst.Close()

	if _, err := io.Copy(dst, file); err != nil {
		global.GVA_LOG.Error("写入文件失败", zap.Error(err))
		response.FailWithMessage("文件保存失败", c)
		return
	}

	// 获取设备ID
	deviceID := c.Query("deviceID")
	if deviceID == "" {
		global.GVA_LOG.Error("缺少设备ID")
		response.FailWithMessage("缺少设备ID", c)
		return
	}

	// 硬编码图片域名
	imageDomain := "http://your-domain.com"
	imageURL := imageDomain + "/" + filePath

	// 更新设备记录中的图片URL
	if err := decimal_deviceService.UploadImage(context.Background(), deviceID, imageURL); err != nil {
		global.GVA_LOG.Error("更新设备记录失败", zap.Error(err))
		response.FailWithMessage("更新设备记录失败", c)
		return
	}

	//response.OkWithData(gin.H{"url": imageURL}, "图片上传成功", c)
}
